<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    function getAllData(){
		$this->db->select('products.code,products.name,products.code,products.products_id');
		$this->db->where("products.products_id <> ",-1); 
        $query = $this->db->get('products');
      
        return $query->result();
    } 
     function get_object($id)
    {
        $query = $this->db->get_where('products', array('products_id' =>$id));
        return $query->row();
    }
	function get_object_from_code($code)
    {
        $query = $this->db->get_where('products', array('code' =>$code));
        return $query->row();
    }
    function getNumData(){
      
		$this->db->where("products.products_id <> ",-1); 
	 	$query = $this->db->get('products');
        return $query->num_rows();
    }
    function delete($id){
          $this->db->where('products_id',$id);
          $query = $this->db->delete('products');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function deleted($id){
          $this->db->where('products_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('products',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }
   
   function saveData($products_id,$data){
             if($products_id!=0){
                    $this->db->where('products_id',$products_id);
                    if($this->db->update('products',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('products',$data)){
                        return $this->db->insert_id();
                        
                    }else{
                        return false;
                    }
            }
    }
	function get_num_sale($arr_search){
		  $this->db->select("products.*,product_category.name as pro_cate_name");
            if(!empty($arr_search)){
			    foreach($arr_search as $key=>$value){
				 if($value==2 && $key=='conhang')
				 	 $this->db->where("products.soluong",$value); 
				 else if($value==1 && $key=='conhang') 
				 	 $this->db->where("products.soluong > ",0); 
				 if($value!='' && $key!='conhang')
				 	$this->db->where("products.$key",$value);  
			   }
		   }
            /*End search*/
		
            $this->db->join('product_category','product_category.product_category_id=products.product_category_id','left');
            $this->db->order_by('products_id','DESC');
			$this->db->where("products.bl_active <> ",-1); 
			$this->db->where("products.sprice >",0);  
            $query = $this->db->get('products');
             return $query->num_rows();
    }
    function get_num_data($arr_search){
		  $this->db->select("products.*,product_category.name as pro_cate_name");
            if(!empty($arr_search)){
			    foreach($arr_search as $key=>$value){
				 if($value==2 && $key=='conhang')
				 	 $this->db->where("products.soluong",$value); 
				 else if($value==1 && $key=='conhang') 
				 	 $this->db->where("products.soluong > ",0); 
				 if($value!='' && $key!='conhang')
				 	$this->db->where("products.$key",$value);  
			   }
		   }
            /*End search*/
		
            $this->db->join('product_category','product_category.product_category_id=products.product_category_id','left');
            $this->db->order_by('products_id','DESC');
			$this->db->where("products.bl_active <> ",-1); 
            $query = $this->db->get('products');
             return $query->num_rows();
    }
    function get_all_data($num,$offset,$arr_search)
    {
           $this->db->select("products.*,product_category.name as pro_cate_name");
		   $this->db->join('product_category','product_category.product_category_id=products.product_category_id','left');
            if(!empty($arr_search)){
			  foreach($arr_search as $key=>$value){
				 if($value==2 && $key=='conhang')
				 	 $this->db->where("products.soluong",$value); 
				 else if($value==1 && $key=='conhang') 
				 	 $this->db->where("products.soluong > ",0); 
				 if($value!='' && $key!='conhang')
				 	$this->db->where("products.$key",$value);  
			   }
		   }
            /*End search*/
			$this->db->where("products.bl_active <> ",-1);  
			
            $this->db->order_by('products_id','DESC');
            $query = $this->db->get('products',$num,$offset);
             return $query->result();
    }
	function get_all_sale($num,$offset,$arr_search)
    {
           $this->db->select("products.*,product_category.name as pro_cate_name");
		   $this->db->join('product_category','product_category.product_category_id=products.product_category_id','left');
            if(!empty($arr_search)){
			  foreach($arr_search as $key=>$value){
				 if($value==2 && $key=='conhang')
				 	 $this->db->where("products.soluong",$value); 
				 else if($value==1 && $key=='conhang') 
				 	 $this->db->where("products.soluong > ",0); 
				 if($value!='' && $key!='conhang')
				 	$this->db->where("products.$key",$value);  
			   }
		   }
            /*End search*/
			$this->db->where("products.bl_active <> ",-1);  
			$this->db->where("products.sprice >",0);  
            $this->db->order_by('products_id','DESC');
            $query = $this->db->get('products',$num,$offset);
             return $query->result();
    }
	
    function getCategoryName($partners_id=0) {
        $this->db->select("name");
        $this->db->where('partners_id',$partners_id);
        $query = $this->db->get("partners");
        return $query->row();
    }
    
  }
?>
